home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / MOR55SRC.ZIP / MORIA / SOURCE / FILES.C < prev    next >
C/C++ Source or Header  |  1992-12-07  |  16KB  |  561 lines

  1. /* source/files.c: misc code to access files used by Moria
  2.  
  3.    Copyright (c) 1989-92 James E. Wilson, Robert A. Koeneke
  4.  
  5.    This software may be copied and distributed for educational, research, and
  6.    not for profit purposes provided that this copyright and statement are
  7.    included in all such copies. */
  8.  
  9. #include <stdio.h>
  10.  
  11. #ifndef STDIO_LOADED
  12. #define STDIO_LOADED
  13. #endif
  14.  
  15. #if 0
  16. /* moved to externs.h to avoid VMS 'psect' problem */
  17. #include <errno.h>
  18. #endif
  19.  
  20. #ifdef __TURBOC__
  21. #include    <io.h>
  22. #include    <stdlib.h>
  23. #endif /* __TURBOC__ */
  24.  
  25. #include "config.h"
  26. #include "constant.h"
  27. #include "types.h"
  28.  
  29. #if defined(GEMDOS) && (__STDC__ == 0) && !defined(ATARIST_TC)
  30. #include <access.h>
  31. char *strcat();
  32. #endif
  33.  
  34. #ifdef VMS
  35. #include <string.h>
  36. #include <file.h>
  37. #else
  38. #ifdef USG
  39. #ifndef ATARIST_MWC
  40. #include <string.h>
  41. #ifndef ATARIST_TC
  42. #include <fcntl.h>
  43. #endif
  44. #endif
  45. #else
  46. #include <strings.h>
  47. #include <sys/file.h>
  48. #endif
  49. #if defined(ultrix) || defined(USG)
  50. void exit();
  51. #endif
  52. #endif
  53.  
  54. /* This must be included after fcntl.h, which has a prototype for `open'
  55.    on some systems.  Otherwise, the `open' prototype conflicts with the
  56.    `topen' declaration.  */
  57. #include "externs.h"
  58.  
  59. #ifdef ATARIST_TC
  60. /* Include this to get prototypes for standard library functions.  */
  61. #include <stdlib.h>
  62. #endif
  63.  
  64. #ifdef MAC
  65. #include "ScrnMgr.h"
  66. #define GNRL_ALRT    1024
  67. #endif
  68.  
  69. /*
  70.  *  init_scorefile
  71.  *  Open the score file while we still have the setuid privileges.  Later
  72.  *  when the score is being written out, you must be sure to flock the file
  73.  *  so we don't have multiple people trying to write to it at the same time.
  74.  *  Craig Norborg (doc)        Mon Aug 10 16:41:59 EST 1987
  75.  */
  76. void init_scorefile()
  77. {
  78. #ifdef MAC
  79.   appldirectory ();
  80. #endif
  81.  
  82. #if defined(atarist) || defined(ATARI_ST) || defined(MAC)
  83.   highscore_fp = fopen(MORIA_TOP, "rb+");
  84. #else
  85.   highscore_fp = fopen(MORIA_TOP, "r+");
  86. #endif
  87.  
  88.   if (highscore_fp == NULL)
  89.     {
  90. #ifdef MAC
  91.       highscore_fp = fopen (MORIA_TOP, "wb");    /* Create it if not there.  */
  92.       if (highscore_fp == NULL)
  93.     {
  94.       ParamText ("\pCan't create score file!", NULL, NULL, NULL);
  95.       DoScreenALRT (GNRL_ALRT, akStop, fixHalf, fixThird);
  96.       ExitToShell ();
  97.     }
  98.       setfileinfo (MORIA_TOP, currentdirectory (), SCORE_FTYPE);
  99. #else
  100.       (void) fprintf (stderr, "Can't open score file \"%s\"\n", MORIA_TOP);
  101.       exit(1);
  102. #endif
  103.     }
  104. #if defined(MSDOS) || defined(VMS) || defined(MAC)
  105.   /* can't leave it open, since this causes problems on networked PCs and VMS,
  106.      we DO want to check to make sure we can open the file, though */
  107.   fclose (highscore_fp);
  108. #endif
  109.  
  110. #ifdef MAC
  111.   restoredirectory ();
  112. #endif
  113. }
  114.  
  115. #ifndef MAC
  116. /* Attempt to open the intro file            -RAK-     */
  117. /* This routine also checks the hours file vs. what time it is    -Doc */
  118. void read_times()
  119. {
  120.   vtype in_line;
  121.   register int i;
  122.   FILE *file1;
  123.  
  124. #ifdef MORIA_HOU
  125.   /* Attempt to read hours.dat.     If it does not exist,       */
  126.   /* inform the user so he can tell the wizard about it     */
  127.   if ((file1 = fopen(MORIA_HOU, "r")) != NULL)
  128.     {
  129.       while (fgets(in_line, 80, file1) != CNIL)
  130.     if (strlen(in_line) > 3)
  131.       {
  132.         if (!strncmp(in_line, "SUN:", 4))
  133.           (void) strcpy(days[0], in_line);
  134.         else if (!strncmp(in_line, "MON:", 4))
  135.           (void) strcpy(days[1], in_line);
  136.         else if (!strncmp(in_line, "TUE:", 4))
  137.           (void) strcpy(days[2], in_line);
  138.         else if (!strncmp(in_line, "WED:", 4))
  139.           (void) strcpy(days[3], in_line);
  140.         else if (!strncmp(in_line, "THU:", 4))
  141.           (void) strcpy(days[4], in_line);
  142.         else if (!strncmp(in_line, "FRI:", 4))
  143.           (void) strcpy(days[5], in_line);
  144.         else if (!strncmp(in_line, "SAT:", 4))
  145.           (void) strcpy(days[6], in_line);
  146.       }
  147.       (void) fclose(file1);
  148.     }
  149.   else
  150.     {
  151.       restore_term();
  152.       (void) fprintf(stderr, "There is no hours file \"%s\".\n", MORIA_HOU);
  153.       (void) fprintf(stderr, "Please inform the wizard, %s, so he ", WIZARD);
  154.       (void) fprintf(stderr, "can correct this!\n");
  155.       exit(1);
  156.     }
  157.  
  158.   /* Check the hours, if closed    then exit. */
  159.   if (!check_time())
  160.     {
  161.       if ((file1 = fopen(MORIA_HOU, "r")) != NULL)
  162.     {
  163.       clear_screen();
  164. #ifdef VMS
  165.       restore_screen();
  166. #endif
  167.       for (i = 0; fgets(in_line, 80, file1) != CNIL; i++)
  168.         put_buffer(in_line, i, 0);
  169.       pause_line (23);
  170.       (void) fclose(file1);
  171.     }
  172.       exit_game();
  173.     }
  174. #endif
  175.  
  176.   /* Print the introduction message, news, etc.         */
  177.   if ((file1 = fopen(MORIA_MOR, "r")) != NULL)
  178.     {
  179.       clear_screen();
  180. #ifdef VMS
  181.       restore_screen();
  182. #endif
  183.       for (i = 0; fgets(in_line, 80, file1) != CNIL; i++)
  184.     put_buffer(in_line, i, 0);
  185.       pause_line(23);
  186.       (void) fclose(file1);
  187.     }
  188. }
  189. #endif
  190.  
  191. /* File perusal.        -CJS-
  192.    primitive, but portable */
  193. void helpfile(filename)
  194. char *filename;
  195. #ifdef MAC
  196. {
  197.   mac_helpfile(filename, TRUE);
  198. }
  199. #else
  200. {
  201.   bigvtype tmp_str;
  202.   FILE *file;
  203.   char input;
  204.   int i;
  205.  
  206.   file = fopen(filename, "r");
  207.   if (file == NULL)
  208.     {
  209.       (void) sprintf (tmp_str, "Can not find help file \"%s\".\n", filename);
  210.       prt (tmp_str, 0, 0);
  211.       return;
  212.     }
  213.  
  214.   save_screen();
  215.  
  216.   while (!feof(file))
  217.     {
  218.       clear_screen();
  219.       for (i = 0; i < 23; i++)
  220.     if (fgets (tmp_str, BIGVTYPESIZ-1, file) != CNIL)
  221.       put_buffer (tmp_str, i, 0);
  222.       prt("[Press any key to continue.]", 23, 23);
  223.       input = inkey();
  224.       if (input == ESCAPE)
  225.     break;
  226.     }
  227.  
  228.   (void) fclose(file);
  229.   restore_screen();
  230. }
  231. #endif
  232.  
  233. /* Prints a list of random objects to a file.  Note that -RAK-     */
  234. /* the objects produced is a sampling of objects which         */
  235. /* be expected to appear on that level.                 */
  236. void print_objects()
  237. {
  238.   register int i;
  239.   int nobj, j, level;
  240.   vtype filename1; bigvtype tmp_str;
  241.   register FILE *file1;
  242.   register inven_type *i_ptr;
  243. #ifdef MAC
  244.   short vrefnum;
  245. #endif
  246. #ifdef ATARIST_MWC
  247.   int32u holder;
  248. #endif
  249.  
  250.   prt("Produce objects on what level?: ", 0, 0);
  251.   level = 0;
  252.   if (!get_string(tmp_str, 0, 32, 10))
  253.     return;
  254.   level = atoi(tmp_str);
  255.   prt("Produce how many objects?: ", 0, 0);
  256.   nobj = 0;
  257.   if (!get_string(tmp_str, 0, 27, 10))
  258.     return;
  259.   nobj = atoi(tmp_str);
  260.   if ((nobj > 0) && (level > -1) && (level < 1201))
  261.     {
  262.       if (nobj > 10000)
  263.     nobj = 10000;
  264. #ifdef MAC
  265.       (void) strcpy(filename1, "Objects");
  266.       if (doputfile("Save objects in:", filename1, &vrefnum))
  267. #else
  268.       prt("File name: ", 0, 0);
  269.       if (get_string(filename1, 0, 11, 64))
  270. #endif
  271.     {
  272.       if (strlen(filename1) == 0)
  273.         return;
  274. #ifdef MAC
  275.       changedirectory(vrefnum);
  276. #endif
  277.       if ((file1 = fopen(filename1, "w")) != NULL)
  278.         {
  279. #ifdef MAC
  280.           macbeginwait ();
  281. #endif
  282.  
  283.           (void) sprintf(tmp_str, "%d", nobj);
  284.           prt(strcat(tmp_str, " random objects being produced..."), 0, 0);
  285.           put_qio();
  286.           (void) fprintf(file1, "*** Random Object Sampling:\n");
  287.           (void) fprintf(file1, "*** %d objects\n", nobj);
  288.           (void) fprintf(file1, "*** For Level %d\n", level);
  289.           (void) fprintf(file1, "\n");
  290.           (void) fprintf(file1, "\n");
  291.           j = popt();
  292.           for (i = 0; i < nobj; i++)
  293.         {
  294.           invcopy(&t_list[j], sorted_objects[get_obj_num(level)]);
  295.           magic_treasure(j, level);
  296.           i_ptr = &t_list[j];
  297.           store_bought(i_ptr);
  298. #ifdef ATARIST_MWC
  299.           if (i_ptr->flags & (holder = TR_CURSED))
  300. #else
  301.           if (i_ptr->flags & TR_CURSED)
  302. #endif
  303.             add_inscribe(i_ptr, ID_DAMD);
  304.           objdes(tmp_str, i_ptr, TRUE);
  305.           (void) fprintf(file1, "%d %s\n", i_ptr->level, tmp_str);
  306.         }
  307.           pusht((int8u)j);
  308.           (void) fclose(file1);
  309. #ifdef MAC
  310.           setfileinfo(filename1, vrefnum, INFO_FTYPE);
  311.           macendwait ();
  312. #endif
  313.           prt("Completed.", 0, 0);
  314.         }
  315.       else
  316.         prt("File could not be opened.", 0, 0);
  317. #ifdef MAC
  318.       restoredirectory();
  319. #endif
  320.     }
  321.     }
  322.   else
  323.     prt ("Parameters no good.", 0, 0);
  324. }
  325.  
  326.  
  327. /* Print the character to a file or device        -RAK-     */
  328. #ifdef MAC
  329. int file_character()
  330. #else
  331. int file_character(filename1)
  332. char *filename1;
  333. #endif
  334. {
  335.   register int i;
  336.   int j, xbth, xbthb, xfos, xsrh, xstl, xdis, xsave, xdev;
  337.   vtype xinfra;
  338.   int fd;
  339.   register FILE *file1;
  340.   bigvtype prt2;
  341.   register struct misc *p_ptr;
  342.   register inven_type *i_ptr;
  343.   vtype out_val, prt1;
  344.   char *p, *colon, *blank;
  345. #ifdef MAC
  346.   vtype filename1;
  347.   short vrefnum;
  348. #endif
  349.  
  350. #ifdef MAC
  351.   (void) makefilename(filename1, "Stats", TRUE);
  352.   if (!doputfile("Save character description in:", filename1, &vrefnum))
  353.     return (FALSE);
  354. #endif
  355.  
  356. #ifndef VMS
  357.   /* VMS creates a new version of a file, so no need to check for rewrite. */
  358. #ifdef MAC
  359.   changedirectory(vrefnum);
  360.   fd = open (filename1, O_WRONLY|O_CREAT|O_TRUNC);
  361.   restoredirectory();
  362.   macbeginwait ();
  363. #else
  364. #if defined(GEMDOS) && (__STDC__ == 0) && !defined(ATARIST_TC)
  365.   if (!access(filename1, AREAD))
  366.     {
  367.       (void) sprintf(out_val, "Replace existing file %s?", filename1);
  368.       if (get_check(out_val))
  369.     fd = creat(filename1, 1);
  370.     }
  371.   else
  372.     fd = creat (filename1, 1);
  373. #else
  374.   fd = open (filename1, O_WRONLY|O_CREAT|O_EXCL, 0644);
  375.   if (fd < 0 && errno == EEXIST)
  376.     {
  377.       (void) sprintf(out_val, "Replace existing file %s?", filename1);
  378.       if (get_check(out_val))
  379.     fd = open(filename1, O_WRONLY, 0644);
  380.     }
  381. #endif
  382. #endif
  383.   if (fd >= 0)
  384.     {
  385.       /* on some non-unix machines, fdopen() is not reliable, hence must call
  386.      close() and then fopen() */
  387.       (void) close(fd);
  388.       file1 = fopen(filename1, "w");
  389.     }
  390.   else
  391.     file1 = NULL;
  392. #else /* VMS */
  393.   fd = -1;
  394.   file1 = fopen (filename1, "w");
  395. #endif
  396.  
  397.   if (file1 != NULL)
  398.     {
  399.       prt("Writing character sheet...", 0, 0);
  400.       put_qio();
  401.       colon = ":";
  402.       blank = " ";
  403. #ifdef MAC
  404.       (void) fprintf(file1, "\n\n");
  405. #else
  406.       (void) fprintf(file1, "%c\n\n", CTRL('L'));
  407. #endif
  408.       (void) fprintf(file1, " Name%9s %-23s", colon, py.misc.name);
  409.       (void) fprintf(file1, " Age%11s %6d", colon, (int)py.misc.age);
  410.       cnv_stat(py.stats.use_stat[A_STR], prt1);
  411.       (void) fprintf(file1, "   STR : %s\n", prt1);
  412.       (void) fprintf(file1, " Race%9s %-23s", colon,
  413.              race[py.misc.prace].trace);
  414.       (void) fprintf(file1, " Height%8s %6d", colon, (int)py.misc.ht);
  415.       cnv_stat(py.stats.use_stat[A_INT], prt1);
  416.       (void) fprintf(file1, "   INT : %s\n", prt1);
  417.       (void) fprintf(file1, " Sex%10s %-23s", colon,
  418.              (py.misc.male ? "Male" : "Female"));
  419.       (void) fprintf(file1, " Weight%8s %6d", colon, (int)py.misc.wt);
  420.       cnv_stat(py.stats.use_stat[A_WIS], prt1);
  421.       (void) fprintf(file1, "   WIS : %s\n", prt1);
  422.       (void) fprintf(file1, " Class%8s %-23s", colon,
  423.              class[py.misc.pclass].title);
  424.       (void) fprintf(file1, " Social Class : %6d", py.misc.sc);
  425.       cnv_stat(py.stats.use_stat[A_DEX], prt1);
  426.       (void) fprintf(file1, "   DEX : %s\n", prt1);
  427.       (void) fprintf(file1, " Title%8s %-23s", colon, title_string());
  428.       (void) fprintf(file1, "%22s", blank);
  429.       cnv_stat(py.stats.use_stat[A_CON], prt1);
  430.       (void) fprintf(file1, "   CON : %s\n", prt1);
  431.       (void) fprintf(file1, "%34s", blank);
  432.       (void) fprintf(file1, "%26s", blank);
  433.       cnv_stat(py.stats.use_stat[A_CHR], prt1);
  434.       (void) fprintf(file1, "   CHR : %s\n\n", prt1);
  435.  
  436.       (void) fprintf(file1, " + To Hit    : %6d", py.misc.dis_th);
  437.       (void) fprintf(file1, "%8sLevel      : %6d", blank, (int)py.misc.lev);
  438.       (void) fprintf(file1, "    Max Hit Points : %6d\n", py.misc.mhp);
  439.       (void) fprintf(file1, " + To Damage : %6d", py.misc.dis_td);
  440.       (void) fprintf(file1, "%8sExperience : %6ld", blank, py.misc.exp);
  441.       (void) fprintf(file1, "    Cur Hit Points : %6d\n", py.misc.chp);
  442.       (void) fprintf(file1, " + To AC     : %6d", py.misc.dis_tac);
  443.       (void) fprintf(file1, "%8sMax Exp    : %6ld", blank, py.misc.max_exp);
  444.       (void) fprintf(file1, "    Max Mana%8s %6d\n", colon, py.misc.mana);
  445.       (void) fprintf(file1, "   Total AC  : %6d", py.misc.dis_ac);
  446.       if (py.misc.lev == MAX_PLAYER_LEVEL)
  447.     (void) fprintf (file1, "%8sExp to Adv : ******", blank);
  448.       else
  449.     (void) fprintf(file1, "%8sExp to Adv : %6ld", blank,
  450.                (int32)(player_exp[py.misc.lev-1]
  451.                    * py.misc.expfact / 100));
  452.       (void) fprintf(file1, "    Cur Mana%8s %6d\n", colon, py.misc.cmana);
  453.       (void) fprintf(file1, "%29sGold%8s %6ld\n\n", blank, colon,
  454.              py.misc.au);
  455.  
  456.       p_ptr = &py.misc;
  457.       xbth = p_ptr->bth + p_ptr->ptohit * BTH_PLUS_ADJ
  458.     + (class_level_adj[p_ptr->pclass][CLA_BTH] * p_ptr->lev);
  459.       xbthb = p_ptr->bthb + p_ptr->ptohit * BTH_PLUS_ADJ
  460.     + (class_level_adj[p_ptr->pclass][CLA_BTHB] * p_ptr->lev);
  461.       /* this results in a range from 0 to 29 */
  462.       xfos = 40 - p_ptr->fos;
  463.       if (xfos < 0)
  464.     xfos = 0;
  465.       xsrh = p_ptr->srh;
  466.       /* this results in a range from 0 to 9 */
  467.       xstl = p_ptr->stl + 1;
  468.       xdis = p_ptr->disarm + 2 * todis_adj() + stat_adj(A_INT)
  469.     + (class_level_adj[p_ptr->pclass][CLA_DISARM] * p_ptr->lev / 3);
  470.       xsave = p_ptr->save + stat_adj(A_WIS)
  471.     + (class_level_adj[p_ptr->pclass][CLA_SAVE] * p_ptr->lev / 3);
  472.       xdev = p_ptr->save + stat_adj(A_INT)
  473.     + (class_level_adj[p_ptr->pclass][CLA_DEVICE] * p_ptr->lev / 3);
  474.  
  475.       (void) sprintf(xinfra, "%d feet", py.flags.see_infra * 10);
  476.  
  477.       (void) fprintf(file1, "(Miscellaneous Abilities)\n\n");
  478.       (void) fprintf(file1, " Fighting    : %-10s", likert(xbth, 12));
  479.       (void) fprintf(file1, "   Stealth     : %-10s", likert(xstl, 1));
  480.       (void) fprintf(file1, "   Perception  : %s\n", likert(xfos, 3));
  481.       (void) fprintf(file1, " Bows/Throw  : %-10s", likert(xbthb, 12));
  482.       (void) fprintf(file1, "   Disarming   : %-10s", likert(xdis, 8));
  483.       (void) fprintf(file1, "   Searching   : %s\n", likert(xsrh, 6));
  484.       (void) fprintf(file1, " Saving Throw: %-10s", likert(xsave, 6));
  485.       (void) fprintf(file1, "   Magic Device: %-10s", likert(xdev, 6));
  486.       (void) fprintf(file1, "   Infra-Vision: %s\n\n", xinfra);
  487.       /* Write out the character's history     */
  488.       (void) fprintf(file1, "Character Background\n");
  489.       for (i = 0; i < 4; i++)
  490.     (void) fprintf(file1, " %s\n", py.misc.history[i]);
  491.       /* Write out the equipment list.         */
  492.       j = 0;
  493.       (void) fprintf(file1, "\n  [Character's Equipment List]\n\n");
  494.       if (equip_ctr == 0)
  495.     (void) fprintf(file1, "  Character has no equipment in use.\n");
  496.       else
  497.     for (i = INVEN_WIELD; i < INVEN_ARRAY_SIZE; i++)
  498.       {
  499.         i_ptr = &inventory[i];
  500.         if (i_ptr->tval != TV_NOTHING)
  501.           {
  502.         switch (i)
  503.           {
  504.           case INVEN_WIELD:    p = "You are wielding";    break;
  505.           case INVEN_HEAD:    p = "Worn on head";    break;
  506.           case INVEN_NECK:    p = "Worn around neck";    break;
  507.           case INVEN_BODY:    p = "Worn on body";    break;
  508.           case INVEN_ARM:    p = "Worn on shield arm";break;
  509.           case INVEN_HANDS:    p = "Worn on hands";    break;
  510.           case INVEN_RIGHT:    p = "Right ring finger";break;
  511.           case INVEN_LEFT:    p = "Left  ring finger";break;
  512.           case INVEN_FEET:    p = "Worn on feet";    break;
  513.           case INVEN_OUTER:    p = "Worn about body";    break;
  514.           case INVEN_LIGHT:    p = "Light source is";    break;
  515.           case INVEN_AUX:    p = "Secondary weapon";    break;
  516.           default: p = "*Unknown value*";     break;
  517.           }
  518.         objdes(prt2, &inventory[i], TRUE);
  519.         (void) fprintf(file1, "  %c) %-19s: %s\n", j+'a', p, prt2);
  520.         j++;
  521.           }
  522.       }
  523.  
  524.       /* Write out the character's inventory.         */
  525. #ifdef MAC
  526.       (void) fprintf(file1, "\n\n");
  527. #else
  528.       (void) fprintf(file1, "%c\n\n", CTRL('L'));
  529. #endif
  530.       (void) fprintf(file1, "  [General Inventory List]\n\n");
  531.       if (inven_ctr == 0)
  532.     (void) fprintf(file1, "  Character has no objects in inventory.\n");
  533.       else
  534.     {
  535.       for (i = 0; i < inven_ctr; i++)
  536.         {
  537.           objdes(prt2, &inventory[i], TRUE);
  538.           (void) fprintf(file1, "%c) %s\n", i+'a', prt2);
  539.         }
  540.     }
  541. #ifndef MAC
  542.       (void) fprintf(file1, "%c", CTRL('L'));
  543. #endif
  544.       (void) fclose(file1);
  545. #ifdef MAC
  546.       setfileinfo(filename1, vrefnum, INFO_FTYPE);
  547.       macendwait ();
  548. #endif
  549.       prt("Completed.", 0, 0);
  550.       return TRUE;
  551.     }
  552.   else
  553.     {
  554.       if (fd >= 0)
  555.     (void) close (fd);
  556.       (void) sprintf (out_val, "Can't open file %s:", filename1);
  557.       msg_print(out_val);
  558.       return FALSE;
  559.     }
  560. }
  561.